﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint.Taxonomy;

namespace SPContrib.SharePoint.Taxonomy.Extensions
{
    /// <summary>
    /// Extensions for the TermSetCollection class
    /// </summary>
    public static class TermSetCollectionExtensions
    {
        /// <summary>
        /// Filters the collection using a criteria
        /// </summary>
        /// <param name="criteria">the criteria to meet</param>
        public static IEnumerable<TermSet> FilterByCriteria(this TermSetCollection instance, Predicate<TermSet> criteria)
        {
            return from ts in instance
                   where criteria(ts)
                   select ts;
        }

        /// <summary>
        /// Filters the collection based on the termset´s group name
        /// </summary>
        /// <param name="name">the name of the group to filter on</param>
        public static IEnumerable<TermSet> FilterByGroupName(this TermSetCollection instance, string name)
        {
            return instance.FilterByCriteria(ts => ts.Group.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
        }
    }
}
